<?php

include_once F_SOURCE . 'ads/ads.model.class.php';
include_once F_SOURCE . 'launch/launch.model.class.php';

/**
 * 广告创意管理CTRL类
 * 
 * @author Dean
 *
 */
class adsCtrl extends Ctrl{
	
	/**
	 * 显示创意列表
	 */
	public function show() {
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		$type = $_GET['type'];
		$typeArr = array('keywords', 'vbc', 'image');
		if (!in_array($type, $typeArr)){
			$type='keywords';
		}		
		
		$adsModel = new adsModel();
		
		$number = $adsModel->countAll($type);
		
		$adsList = $adsModel->findAll(($pageIndex-1)*PageNum, PageNum, $type);	
		
		$page = pageCount($number, $pageIndex, '?do=ads.show'); 
		
		$data = array(
			'op_select' => array('ads'=>"class=select"),
			'adsList' => $adsList,
			'page' => $page
		);
		
		$view->display($data, 'ads_show.html');
	}
	
	/**
	 * 查询单个投放信息
	 */
	public function info(){
		
		$view = new View();
		
		$id = $_GET['id'];
		
		$adsModel = new adsModel();
		$adsInfo = $adsModel->findOne($id);	
		
		$data = array(
			'op' => 'update',
			'op_select' => array('ads'=>"class=select"),
			'obj' => $adsInfo
		);
		
		$view->display($data, 'ads_view.html');
	}
	
	/**
	 * 跳转新增页面
	 */
	public function view(){
		$view = new View();
		
		$data = array(
			'op' => $_GET['op'],
			'op_select' => array('ads'=>"class=select")
		);
		
		$view->display($data, 'ads_view.html');
	}
	
	/**
	 * 新增投放操作
	 * 
	 */
	public function add(){
		
		$id = getPostIntParam('id');
		$adsId = getPostIntParam('adsId');
		$adsname =  getPostStringParam('name');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$keywords = getPostStringParam('keywords');
		
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
		
		$data = array(
			'ads_id' => $adsId,
			'name' => $adsname,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'keywords' => $keywords,
			'createTime' => $time,
			'modifyTime' => $time,
			'createUser' => $createUser
		);

		$launchModel = new launchModel();
		$insertID = $launchModel->add($data);
		
			
		if($insertID > 0){
			$adsData = array(
				'op' => 'c'		
			);
			$where = array(
				'id' => $id
			);
			$adsModel = new adsModel();
			$number = $adsModel->modify($adsData, $where);
			
			$this->showMessage('?do=ads.show', 'op_success');
		}else{
			$this->showMessage('?do=ads.view&op=add', 'op_failure');
		}
	}

}


?>